﻿XIncludeFile "Function.pb"

;==============================函数类型===============================
Prototype.l TextSize(This,Width,Height)
Prototype.l TextText(This,Text.s)
Prototype.l TextClick(This,hWnd, uMsg, wParam, lParam)


;==================================常数==================================
Enumeration
 #Style=1
 #Visible=2
 #Enabled=4
EndEnumeration

;==============================窗口结构==============================
Structure GdipText Extends PWnd
  Style.l
  PassWord.c
EndStructure

;================================函数===========================================

Procedure SetTextStareImage(*Text.GdipText,Stare=0)
 With *Text\Creat
  GdipDisposeImage(\ImageBack)   ;绘制缓冲
  GdipDisposeImage(\Center)   ;绘制缓冲
  GdipDisposeImage(\Left)   ;绘制缓冲
  GdipDisposeImage(\DownLeft)   ;绘制缓冲
  GdipDisposeImage(\Down)   ;绘制缓冲
  GdipDisposeImage(\DownRight)   ;绘制缓冲
  GdipDisposeImage(\Right)   ;绘制缓冲
  GdipDisposeImage(\UpRight)   ;绘制缓冲
  GdipDisposeImage(\Up)   ;绘制缓冲
  GdipDisposeImage(\UpLeft)   ;绘制缓冲
  If *Text\enabled=3
      \UpLeft=GdipCatchImage(?ImageTextFlase_UpLeft,?ImageTextFlase_Up-?ImageTextFlase_UpLeft)
      \Up=GdipCatchImage(?ImageTextFlase_Up,?ImageTextFlase_UpRight-?ImageTextFlase_Up)
      \UpRight=GdipCatchImage(?ImageTextFlase_UpRight,?ImageTextFlase_Right-?ImageTextFlase_UpRight)
      \Right=GdipCatchImage(?ImageTextFlase_Right,?ImageTextFlase_DownRight-?ImageTextFlase_Right)
      \DownRight=GdipCatchImage(?ImageTextFlase_DownRight,?ImageTextFlase_Down-?ImageTextFlase_DownRight)
      \Down=GdipCatchImage(?ImageTextFlase_Down,?ImageTextFlase_DownLeft-?ImageTextFlase_Down)
      \DownLeft=GdipCatchImage(?ImageTextFlase_DownLeft,?ImageTextFlase_Left-?ImageTextFlase_DownLeft)
      \Left=GdipCatchImage(?ImageTextFlase_Left,?ImageTextFlase_Center-?ImageTextFlase_Left)
      \Center=GdipCatchImage(?ImageTextFlase_Center,?ImageTextEnd-?ImageTextFlase_Center)
  Else 
      \UpLeft=GdipCatchImage(?ImageTextUp_UpLeft,?ImageTextUp_Up-?ImageTextUp_UpLeft)
      \Up=GdipCatchImage(?ImageTextUp_Up,?ImageTextUp_UpRight-?ImageTextUp_Up)
      \UpRight=GdipCatchImage(?ImageTextUp_UpRight,?ImageTextUp_Right-?ImageTextUp_UpRight)
      \Right=GdipCatchImage(?ImageTextUp_Right,?ImageTextUp_DownRight-?ImageTextUp_Right)
      \DownRight=GdipCatchImage(?ImageTextUp_DownRight,?ImageTextUp_Down-?ImageTextUp_DownRight)
      \Down=GdipCatchImage(?ImageTextUp_Down,?ImageTextUp_DownLeft-?ImageTextUp_Down)
      \DownLeft=GdipCatchImage(?ImageTextUp_DownLeft,?ImageTextUp_Left-?ImageTextUp_DownLeft)
      \Left=GdipCatchImage(?ImageTextUp_Left,?ImageTextUp_Center-?ImageTextUp_Left)
      \Center=GdipCatchImage(?ImageTextUp_Center,?ImageTextFlase_UpLeft-?ImageTextUp_Center)
  EndIf
 EndWith
EndProcedure

Procedure DrawTextStare(*Text.GdipText)
  GdipDisposeImage(*Text\Creat\ImageBack) 
  If *Text\Image
     GdipCreateBitmapFromScan0(*Text\Width,*Text\Height,0,#GDIPLUS_PIXELFORMAT_32bppARGB, 0,@*Text\Creat\ImageBack)
     GdipGetImageGraphicsContext(*Text\Creat\ImageBack,@Graphics)
     If *Text\Enabled =3  
       GdipDrawImageRectRectI(Graphics, *Text\Image, 0, 0,*Text\Width,*Text\Height, *Text\Width, 0, *Text\Width,*Text\Height, #UnitPixel,#Null, #Null, #Null)
     Else
       GdipDrawImageRectRectI(Graphics, *Text\Image, 0, 0,*Text\Width,*Text\Height, *Text\Width * *Text\Enabled, 0, *Text\Width,*Text\Height, #UnitPixel,#Null, #Null, #Null)
     EndIf 
     GdipDeleteGraphics(Graphics)
  Else  
     SetTextStareImage(*Text,*Text\Enabled)
     GdipCreateBitmapFromScan0(*Text\Width,*Text\Height,0,#GDIPLUS_PIXELFORMAT_32bppARGB, 0,@*Text\Creat\ImageBack)
     *Text\wSize(*Text,*Text\Width,*Text\Height)
  EndIf 
EndProcedure

Procedure ITextText(*Text.GdipText,Text.s)
  With *Text\Creat
    \Text\Text.s=Text
    TextColor=\Text\TextColor
    PeColor=\Text\PenColor
    If *Text\Enabled =3 
      TextColor=$AA505050
      PenColor= $50505050
    EndIf 
    If *Text\PassWord
       Text =LSet("",Len(Text),Chr(*Text\PassWord))
    EndIf 
    If *Text\Image
      DrawTextStare(*Text)
      GdipGetImageGraphicsContext(\ImageBack,@Graphics)
      GdipPathText(Graphics,Text,\Text\X,\Text\Y,\Text\Width,\Text\Height,\Text\FontName,\Text\FontSize,\Text\FontStyle,TextColor,PenColor,\Text\PenWidth,\Text\Align,\Text\Style)
      GdipDeleteGraphics(Graphics)
    Else
      GdipGetImageWidth(\Left,@X)
      GdipGetImageHeight(\Up,@Y)
      GdipGetImageWidth(\ImageBack,@Width)
      GdipGetImageHeight(\ImageBack,@Height)
      Width -X-GdipImageWidth (\Right)
      Height-Y-GdipImageHeight(\Down)
      GdipGetImageGraphicsContext(\ImageBack,@Graphics)
        GdipSetCompositingMode(Graphics,#CompositingModeSourceCopy)
        GdipImageTile(Graphics,\Center,X,Y,Width,Height)
        GdipPathText(Graphics,Text,X+1,Y+1,Width+3,Height,\Text\FontName,\Text\FontSize,\Text\FontStyle,TextColor,PenColor,\Text\PenWidth,\Text\Align,\Text\Style)
      GdipDeleteGraphics(Graphics)
    EndIf
  EndWith
EndProcedure

Procedure ITextSize(*Text.GdipText,Width,Height)
  *Text\Width =Width
  *Text\Height=Height
  With *Text\Creat
    GdipDisposeImage(\ImageBack)
    \ImageBack=GdipCreateImage(Width,Height)
     GdipGetImageGraphicsContext(\ImageBack,@Graphics)
      GdipSetCompositingMode(Graphics,#CompositingModeSourceCopy)
      GdipImageDraw(Graphics,\UpLeft,0,0)
      GdipImageTile(Graphics,\Up,GdipImageWidth(\UpLeft),0,Width-GdipImageWidth(\UpLeft)-GdipImageWidth(\UpRight),GdipImageHeight(\Up))
      GdipImageDraw(Graphics,\UpRight,Width-GdipImageWidth(\UpRight),0)
      GdipImageTile(Graphics,\Right,Width-GdipImageWidth(\Right),GdipImageHeight(\UpRight),GdipImageWidth(\Right),Height-GdipImageHeight(\UpRight)-GdipImageHeight(\DownRight))
      GdipImageDraw(Graphics,\DownRight,Width-GdipImageWidth(\DownRight),Height-GdipImageHeight(\DownRight)) 
      GdipImageTile(Graphics,\Down,GdipImageWidth(\DownLeft),Height-GdipImageHeight(\Down),Width-GdipImageWidth(\DownLeft)-GdipImageWidth(\DownRight),GdipImageHeight(\Down))
      GdipImageDraw(Graphics,\DownLeft,0,Height-GdipImageHeight(\DownLeft))
      GdipImageTile(Graphics,\Left,0,GdipImageHeight(\UpLeft),GdipImageWidth(\Left),Height-GdipImageHeight(\Upleft)-GdipImageHeight(\DownLeft))
    GdipDeleteGraphics(Graphics)
  EndWith
  *Text\wText(*Text,*Text\Creat\Text\Text)
  ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure

Procedure ITextLoad(*Text.GdipText)
  DrawTextStare(*Text)
  *Text\wText(*Text,*Text\Creat\Text\Text)
EndProcedure

Procedure ITextDraw(*Text.GdipText,*Gadget.PWnd,*cRect.RECT)

EndProcedure

Procedure ITextEvent(*Text.GdipText,hWnd, uMsg, wParam, lParam)
  If *Text\Enabled =3
    ProcedureReturn  1
  EndIf 
  Select  uMsg
    Case #WM_CHAR
      Text.s =*Text\Creat\Text\Text
      Select wParam
        Case 8
          Text= Left(Text,Len(Text)-1) 
        ;EndCase
        Case 22
          If Not *Text\PassWord
             Text +GetClipboardText()
          EndIf 
        ;EndCase
        Default
          Text + Chr(wParam)
      EndSelect 
      If Text <> *Text\Creat\Text\Text
        *Text\wText(*Text, Text)
        SendMessage_(Hwnd,#WM_Draw,*Text,@*Text\X)
      EndIf 
   ;EndCase
   Case #WM_LBUTTONDOWN
      *Window.PWnd=*Text\HostGadget
      If *Window
        *Window\OnGadget=*Text
      EndIf
   ;EndCase
  EndSelect
;    Debug *Text\Creat\text\text
  ProcedureReturn  1
EndProcedure

Procedure ITextIsLoca(*Text.GdipText,hWnd,X,Y)
  X-*Text\X
  Y-*Text\Y
  If X>-1 And Y>-1 And X<*Text\Width And Y<*Text\Height  And *Text\Visible
     GdipBitmapGetPixel(*Text\Creat\ImageBack,X,Y,@Color)
     If GdipAlpha(Color)
        ProcedureReturn *Text
     EndIf 
  EndIf 
EndProcedure

Procedure ITextFree(*Text.GdipText)
  GdipDisposeImage(*Text\Image)  ;当前绘制的图片
  GdipDisposeImage(*Text\Draw)
  GdipDisposeImage(*Text\Creat\ImageBack)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\Center)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\Left)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\DownLeft)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\Down)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\DownRight)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\Right)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\UpRight)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\Up)   ;绘制缓冲
  GdipDisposeImage(*Text\Creat\UpLeft)   ;绘制缓冲
  DeleteWindowList(*Text)
EndProcedure

Procedure GdipCreateText(*Hwnd.PWnd,Image,X,Y,Width,Height,Text.s="",Style=6)  ;创建文本输入框
 *Text.GdipText=AddWindowList(@*Hwnd\Gadget,SizeOf(GdipText))
 If Image
    GdipCloneImage(Image,@*Text\Image)
    GdipGetImageWidth(Image,@Width )
    GdipGetImageHeight(Image,@Height)
    GdipCloneImage(Image,@*Text\Creat\ImageBack)
 Else 
   With *Text\Creat
      SetTextStareImage(*Text)
      iWidth =GdipImageWidth(\UpLeft)+GdipImageWidth(\UpRight)
      iHeight=GdipImageHeight(\Up)    +GdipImageHeight(\Down)
      If Width <iWidth : Width =iWidth : EndIf
      If Height<iHeight: Height=iHeight: EndIf
      \ImageBack=GdipCreateImage(Width,Height)
    EndWith
 EndIf 
 With *Text\Creat\Text
   \Text=Text
   \FontName="宋体"
   \FontSize=15
   \FontStyle=1
   \TextColor=$FF000000
   \Align= #GD_TextLineAlignCenter 
   \Style=4
   \X     = 0
   \Y     = 0
   \Width = Width/2
   \Height= Height
 EndWith
 With *Text
    \X=X
    \Y=Y
    If Image
      \Width=Width/2
    Else 
      \Width=Width
    EndIf 
    \Height=Height
    \Visible=IsVal(Style,#Visible)
    \Enabled=(1-IsVal(Style,#Enabled))*3
    \Type=#Text
    \Style=IsVal(Style,1)
    \HostGadget=*Hwnd
    *Hwnd\NewGadget= *Text
    \wIsLoca=@ITextIsLoca()
    \wLoad=@ITextLoad()
    \wDraw=@ITextDraw()
    \wEvent=@ITextEvent()
    \wFree=@ITextFree()
    \wSize=@ITextSize()
    \wText=@ITextText()   
  EndWith
  ProcedureReturn  *Text
EndProcedure

Procedure GdipTextPassWord(*Text.GdipText,PassWord.C)
 *Text\PassWord=PassWord
EndProcedure 

DataSection
ImageTextUp_UpLeft:
  IncludeBinary "Image\Text\Up\UpLeft.png"
ImageTextUp_Up:
  IncludeBinary "Image\Text\Up\Up.png"
ImageTextUp_UpRight:
  IncludeBinary "Image\Text\Up\UpRight.png"
ImageTextUp_Right:
  IncludeBinary "Image\Text\Up\Right.png"
ImageTextUp_DownRight:
  IncludeBinary "Image\Text\Up\DownRight.png"
ImageTextUp_Down:
  IncludeBinary "Image\Text\Up\Down.png"
ImageTextUp_DownLeft:
  IncludeBinary "Image\Text\Up\DownLeft.png"
ImageTextUp_Left:
  IncludeBinary "Image\Text\Up\Left.png"
ImageTextUp_Center:
  IncludeBinary "Image\Text\Up\Center.png"

ImageTextFlase_UpLeft:
  IncludeBinary "Image\Text\Flase\UpLeft.png"
ImageTextFlase_Up:
  IncludeBinary "Image\Text\Flase\Up.png"
ImageTextFlase_UpRight:
  IncludeBinary "Image\Text\Flase\UpRight.png"
ImageTextFlase_Right:
  IncludeBinary "Image\Text\Flase\Right.png"
ImageTextFlase_DownRight:
  IncludeBinary "Image\Text\Flase\DownRight.png"
ImageTextFlase_Down:
  IncludeBinary "Image\Text\Flase\Down.png"
ImageTextFlase_DownLeft:
  IncludeBinary "Image\Text\Flase\DownLeft.png"
ImageTextFlase_Left:
  IncludeBinary "Image\Text\Flase\Left.png"
ImageTextFlase_Center:
  IncludeBinary "Image\Text\Flase\Center.png"

ImageTextEnd:
EndDataSection
; IDE Options = PureBasic 4.30 (Windows - x86)
; CursorPosition = 260
; FirstLine = 100
; Folding = Ke9-9-
; EnableAsm
; EnableUnicode
; EnableXP
; EnableOnError
; HideErrorLog
; CompileSourceDirectory